package com.bss.framework;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageServlet extends HttpServlet {

	private ImageController imageController;

	/**
	 * initialization method for servlet
	 */
	public void init() {
		System.out.println("IMAGE......");
		imageController = new ImageController();
	}

	/**
	 * The doget method of the servlet
	 * 
	 * @param request
	 *            HttpServletRequest object
	 * @param response
	 *            HttpServletResponse object
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException,
			java.io.IOException {

		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {	
		
		FrontController.debug("Image:  ...."+request.getRequestURI());
		System.out.println("Image:  ...."+request.getRequestURI());
		try {
			if (request.getRequestURI().endsWith("click")) {
				imageController.click(request, response);
			} else if (request.getRequestURI().contains("clicked")) {
				imageController.clicked(request, response);
			} else if (request.getRequestURI().contains("saved")) {
				imageController.saved(request, response);
			} else {
				FrontController.error("Error while image operation-Unknown");
				response.setStatus(HttpServletResponse.SC_NO_CONTENT);
			}
		} catch (Exception e) {
			FrontController.error("Error while image operation: "+e.getMessage());
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
		}
	}

}